package com.nanjia.boot.certificatetype.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.certificatetype.model.form.CertificateTypeForm;
import com.nanjia.boot.certificatetype.model.query.CertificateTypeQuery;
import com.nanjia.boot.certificatetype.model.vo.CertificateTypeVO;
import com.nanjia.boot.certificatetype.service.CertificateTypeService;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 证件类型前端控制层
 *
 * @author 周洋
 * @since 2025-06-13 10:14
 */
@Tag(name = "证件类型接口")
@RestController
@RequestMapping("/api/v1/certificate-type")
@RequiredArgsConstructor
public class CertificateTypeController {

    private final CertificateTypeService certificateTypeService;

    @Operation(summary = "证件类型分页列表")
    @GetMapping("/page")
    // @PreAuthorize("@ss.hasPerm('certificateType:certificate-type:query')")
    public PageResult<CertificateTypeVO> getCertificateTypePage(CertificateTypeQuery queryParams) {
        IPage<CertificateTypeVO> result = certificateTypeService.getCertificateTypePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增证件类型")
    @PostMapping
    // @PreAuthorize("@ss.hasPerm('certificateType:certificate-type:add')")
    public Result<Void> saveCertificateType(@RequestBody @Valid CertificateTypeForm formData, HttpServletRequest request) {
        boolean result = certificateTypeService.saveCertificateType(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取证件类型表单数据")
    @GetMapping("/{id}/form")
    // @PreAuthorize("@ss.hasPerm('certificateType:certificate-type:edit')")
    public Result<CertificateTypeForm> getCertificateTypeForm(
            @Parameter(description = "证件类型ID") @PathVariable Long id
    ) {
        CertificateTypeForm formData = certificateTypeService.getCertificateTypeFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改证件类型")
    @PutMapping(value = "/{id}")
    // @PreAuthorize("@ss.hasPerm('certificateType:certificate-type:edit')")
    public Result<Void> updateCertificateType(
            @Parameter(description = "证件类型ID") @PathVariable Long id,
            @RequestBody @Validated CertificateTypeForm formData, HttpServletRequest request
    ) {
        boolean result = certificateTypeService.updateCertificateType(id, formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "删除证件类型")
    @DeleteMapping("/{ids}")
    // @PreAuthorize("@ss.hasPerm('certificateType:certificate-type:delete')")
    public Result<Void> deleteCertificateTypes(
            @Parameter(description = "证件类型ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = certificateTypeService.deleteCertificateTypes(ids);
        return Result.judge(result);
    }
}
